<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-7" />
<title>List Appointment</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>

<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>

<script type="text/javascript">
	droplinemenu.buildmenu("nav");
	jQuery(document).ready(function(){
		var datepicker_CurrentInput;  
        $.datepicker.setDefaults({ showButtonPanel: true, closeText: 'clear', beforeShow: function (input, inst) { datepicker_CurrentInput = input; } });  
        $(".ui-datepicker-close").live("click", function (){  
            datepicker_CurrentInput.value = "";  
        });  
		$("#startDate").datepicker({
			dateFormat:'dd/mm/yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043'
		});

		$("#endDate").datepicker({
			dateFormat:'dd/mm/yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043'
		});
	});



	function appointmentFilter(){
		alert("678");
		var startDateObject = document.getElementById("startDate");
		var endDateObject = document.getElementById("endDate");
		var startDate = startDateObject.value;
		var endDate = endDateObject.value;


		if( startDate == "" && endDate == "" ){
			document.getElementById("errorMsg").innerHTML = "please input date!!!";
			startDateObject.focus();
			return;
		}
		alert("456");
		var startMill = 0;
		var endMill = 0;

		if( startDate != "" ){
			var startDateArray = startDate.split("/");
			startDate = startDateArray[1] + "/" + startDateArray[0] + "/" + startDateArray[2];
			alert("start date=" + startDate);
			var startDateObject = new Date(startDate);
			startMill = startDateObject.getTime();
		}
		alert("123");
		if( endDate == "" ){
			var endDateObject = new Date();
			endMill = endDateObject.getTime();
		}else{
			var endDateArray = endDate.split("/");
			endDate = endDateArray[1] + "/" + endDateArray[0] + "/" + endDateArray[2];
			var endDateObject = new Date(endDate);
			endMill = endDateObject.getTime();
		}
		alert("start=" + startMill);
		alert("end=" + endMill);
		if( startMill > endMill ){
			document.getElementById("errorMsg").innerHTML = "end should be after the start!";
			startDateObject.focus();
			return;
		}

		document.getElementById("appointmentForm").submit();
	}
</script>


</head>

<body>
	<div id="container">
		<div id="header"></div>
		<?php include_once 'nav.php';?>
		<?php 
			$ini_array = parse_ini_file("config/config.ini");
		
			date_default_timezone_set($ini_array["default_timezone"]);	
		
			$filterResult;
			$startDate = "";
			$endDate = "";
			if( isset($_POST["startDate"]) || isset($_POST["endDate"]) ){
				if( isset($_POST["startDate"]) && strcmp($_POST["startDate"],"") != 0){
					$startDate = $_POST["startDate"];
				}else{
					$startDate = "01/01/1970";
				}
			
				if( isset($_POST["endDate"]) &&  strcmp($_POST["endDate"],"") != 0 ){
					$endDate = $_POST["endDate"];
				}else{
					$endDate = date("d/m/Y");
			
				}
				$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the ");
				mysqli_set_charset($conn, "utf8");
				ini_set('default_charset', "UTF-8");
				$startDate = mysqli_real_escape_string($conn,$startDate);
				$endDate = mysqli_real_escape_string($conn, $endDate);
			
			
			
			
				$filterSql = "select appointment_no, patient.Patient_no, First_name, Surname, appointment_datetime,landline, mobile, address from appointment, patient where appointment.Patient_no=patient.Patient_no AND appointment_datetime > STR_TO_DATE('".$startDate."','%d/%m/%Y') AND appointment_datetime < STR_TO_DATE('".$endDate."','%d/%m/%Y')  order by appointment_datetime asc";
			
				$filterResult = mysqli_query($conn, $filterSql);
				$resultNumber = mysqli_num_rows($filterResult);
			
			}else{
				$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the ");
				mysqli_set_charset($conn, "utf8");
				ini_set('default_charset', "UTF-8");
				$currentDate = date("Y-m");
				$filterSql = "select appointment_no, patient.Patient_no, First_name, Surname, appointment_datetime,landline, mobile, address from appointment, patient where appointment.Patient_no=patient.Patient_no  order by appointment_datetime asc";
			
				$filterResult = mysqli_query($conn, $filterSql);
				$resultNumber = mysqli_num_rows($filterResult);
			
			}
				
			
		
			
		?>
		<div id="search">
			<form  name="form1" method="post" action="list_appointment.php" id="appointmentForm">
				<label>Start Date:</label><input type="text" name="startDate" id="startDate" value="<?php echo $startDate?>"  readonly="readonly"/>
				<label>End Date:</label><input type="text" name="endDate" id="endDate" value="<?php echo $endDate?>" readonly="readonly"/>
				<input type="button" name="filterAppointment" value="Search" onclick="appointmentFilter()" />
				<label id="errorMsg"></label>
			</form>
			
		</div>
		<div id="main_content" style="text-align:left;" >
			<div id="title" style="text-align:center;"><h2>List of Appointments</h2></div>
			<div style="text-align:left;">
				<div style="text-align:right;width:67px;display:inline-block;"><label>No.</label></div>
				<div style="text-align:right;width:110px;display:inline-block;"><label>Patient</label></div>
				<div style="text-align:right;width:200px;display:inline-block;"><label>Landline</label></div>
				<div style="text-align:right;width:90px;display:inline-block;"><label>Mobile</label></div>
				<div style="text-align:right;width:108px;display:inline-block;"><label>Address</label></div>
				<div style="text-align:right;width:248px;display:inline-block;"><label>Date</label></div>
				<div style="text-align:right;width:88px;display:inline-block;"><label>Time</label></div>
				<div style="text-align:right;width:65px;display:inline-block;"><label>Doctor</label></div>
				<div style="text-align:right;width:74px;display:inline-block;"><label>View</label></div>
			</div>
			<div id="appointmentList" style="max-height:600px;overflow:auto;width:1120px;margin:0px auto;">
				<table style="width:1100px;border-collapse:collapse;">
					<?php 
						while( $row = mysqli_fetch_array($filterResult)){

							$appointmentNo = $row["appointment_no"];
							$firstName = $row["First_name"];
							if( strlen($firstName) > 15 ){
								$firstName = substr($firstName, 0, 15);
							}
							
							$patientNo = $row["Patient_no"];
							
							$surname = $row["Surname"];
							if( strlen($surname) > 15 ){
								$surname = substr($surname, 0, 15);
							}
							$landline = $row["landline"];
							$outputLandline = "";
							for( $i = 0; $i < strlen($landline); $i++ ){
								if( $i%13 == 0 && $i != 0 ){
									$outputLandline .= "\r\n";
									$outputLandline .= $landline[$i];
								}else{
									$outputLandline .= $landline[$i];
								}
							}
							$mobile = $row["mobile"];
							$outputMobile = "";
							for( $i = 0; $i < strlen($mobile); $i++ ){
								if( $i%13 == 0 && $i != 0 ){
									$outputMobile .= "\r\n";
									$outputMobile .= $mobile[$i];
								}else{
									$outputMobile .= $mobile[$i];
								}
							}
							$address = $row["address"];
							if( strlen($address) > 35 ){
								$address = substr($address, 0, 35);
							}
							$date = "";
							$time = "";
							if( strcmp($row["appointment_datetime"], "") != 0 ){
								$date = date("d/m/Y",strtotime($row["appointment_datetime"]));
								$time = date("H:i", strtotime($row["appointment_datetime"]));
							}
							
					?>
					<tr>
						<td width="8%" style=""><?php echo $appointmentNo?></td>
						<td width="18%"style=""><a href="patient_account.php?patientNo=<?php echo $patientNo ?>"><?php echo $firstName."  ".$surname?></a></td>
						<td width="10%" style=""><?php echo $outputLandline?></td>
						<td width="10%" style=""><?php echo $outputMobile?></td>
						<td width="25%" style=""><?php echo $address?></td>
						<td width="9%" style=""><?php echo $date?></td>
						<td width="5%" style=""><?php echo $time?></td>
						<td width="9%" style=""><?php echo "Geraldine"?></td>
						<td width="5%" style=""><a href="appointment.php?appointment_no=<?php echo $appointmentNo?>">view</a></td>
					</tr>
					<?php 
						}
					?>
				</table>
			</div>
			<div style="text-align:center;">
				<input type="button" value="return" onclick="window.location.href='index.php'"/>
			</div>
		</div>
		<?php include_once 'footer.php';?>
	</div>
</body>
</html>
